package util;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import ui.AboutUI;
import ui.CustomerUI;
import ui.EmployeeUI;
import ui.HelpUI;
import ui.ItemUI;
import ui.LoginUI;
import ui.MenuUI;
import ui.OfferUI;
import ui.ProjectUI;
import ui.RequestsUI;
import ui.StatisticsUI;
import ui.SubContractorUI;
import ui.SupplierUI;

import java.awt.Window;

import javax.swing.AbstractAction;

public class Menu extends AbstractAction implements ActionListener 
{

	/**
	 * 
	 */
	private static final long serialVersionUID = -1082557586851452198L;
	// might need to make a boolean, turn it to true when one of the items here is pressed to close the current window
	public static Menu instance = null;

	private JMenuBar menuBar = null;

	private JMenu navigate = null;
	private JMenu help = null;

	private JMenuItem logOut = null;
	private JMenuItem mainMenu = null;

	private JMenu goTo = null;
	private JMenu people = null;
	private JMenu items = null;

	private JMenuItem statistics = null;

	private JMenuItem employees = null;
	private JMenuItem customers = null;
	private JMenuItem suppliers = null;
	private JMenuItem subContractors = null;

	private JMenuItem materials = null;
	private JMenuItem projects = null;
	private JMenuItem offers = null;
	private JMenuItem requests = null;

	private JMenuItem helpContents = null;
	private JMenuItem about = null;

	private static String location = "img/menu_icons/";
	
	public static Menu getInstance()
	{
		if(instance == null)
			instance = new Menu();

		return instance;
	}

	public Menu()
	{		
		menuBar = new JMenuBar();
		navigate = new JMenu("Navigation");
		
		help = new JMenu("Help");
		
		logOut = new JMenuItem("Log Out", KeyEvent.VK_L);
		logOut.setIcon(new ImageIcon((location + "logout.png/")));
		logOut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.ALT_MASK));
		logOut.getAccessibleContext().setAccessibleDescription("ALT+L");
		logOut.addActionListener(this);

		mainMenu = new JMenuItem("Menu", KeyEvent.VK_M);
		mainMenu.setIcon(new ImageIcon((location + "menu.png/")));
		mainMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M, ActionEvent.ALT_MASK));
		mainMenu.getAccessibleContext().setAccessibleDescription("ALT+M");

		goTo = new JMenu("Go To");
		goTo.setIcon(new ImageIcon((location + "goto.png/")));
		
		people = new JMenu("People");
		people.setIcon(new ImageIcon((location + "people.png/")));
		
		items = new JMenu("Items");
		items.setIcon(new ImageIcon((location + "items.png/")));
		
		statistics = new JMenuItem("Statistics", KeyEvent.VK_S);
		statistics.setIcon(new ImageIcon((location + "statistics.png/")));
		statistics.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.ALT_MASK));
		statistics.getAccessibleContext().setAccessibleDescription("ALT+S");
		statistics.addActionListener(this);

		employees = new JMenuItem("Employees", KeyEvent.VK_E);
		employees.setIcon(new ImageIcon((location + "employees.png/")));
		employees.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, ActionEvent.ALT_MASK));
		employees.getAccessibleContext().setAccessibleDescription("ALT+E");
		employees.addActionListener(this);
		
		customers = new JMenuItem("Customers", KeyEvent.VK_C);
		customers.setIcon(new ImageIcon((location + "customers.png/")));
		customers.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.ALT_MASK));
		customers.getAccessibleContext().setAccessibleDescription("ALT+C");
		customers.addActionListener(this);
		
		suppliers = new JMenuItem("Suppliers", KeyEvent.VK_X);
		suppliers.setIcon(new ImageIcon((location + "suppliers.png/")));
		suppliers.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.ALT_MASK));
		suppliers.getAccessibleContext().setAccessibleDescription("ALT+X");
		suppliers.addActionListener(this);

		subContractors = new JMenuItem("SubContractors", KeyEvent.VK_Z);
		subContractors.setIcon(new ImageIcon((location + "subcontractors.png/")));
		subContractors.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, ActionEvent.ALT_MASK));
		subContractors.getAccessibleContext().setAccessibleDescription("ALT+Z");
		subContractors.addActionListener(this);

		materials = new JMenuItem("Materials", KeyEvent.VK_I);
		materials.setIcon(new ImageIcon((location + "materials.png/")));
		materials.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, ActionEvent.ALT_MASK));
		materials.getAccessibleContext().setAccessibleDescription("ALT+I");
		materials.addActionListener(this);

		projects = new JMenuItem("Projects", KeyEvent.VK_P);
		projects.setIcon(new ImageIcon((location + "projects.png/")));
		projects.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.ALT_MASK));
		projects.getAccessibleContext().setAccessibleDescription("ALT+P");
		projects.addActionListener(this);

		offers = new JMenuItem("Offers", KeyEvent.VK_O);
		offers.setIcon(new ImageIcon((location + "offers.png/")));
		offers.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.ALT_MASK));
		offers.getAccessibleContext().setAccessibleDescription("ALT+O");
		offers.addActionListener(this);

		requests = new JMenuItem("Requests", KeyEvent.VK_R);
		requests.setIcon(new ImageIcon((location + "requests.png/")));
		requests.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.ALT_MASK));
		requests.getAccessibleContext().setAccessibleDescription("ALT+R");
		requests.addActionListener(this);

		helpContents = new JMenuItem("Help", KeyEvent.VK_H);
		helpContents.setIcon(new ImageIcon((location + "help.png/")));
		helpContents.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, ActionEvent.ALT_MASK));
		helpContents.getAccessibleContext().setAccessibleDescription("ALT+H");
		helpContents.addActionListener(this);

		about = new JMenuItem("About", KeyEvent.VK_A);
		about.setIcon(new ImageIcon((location + "about.png/")));
		about.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.ALT_MASK));
		about.getAccessibleContext().setAccessibleDescription("ALT+A");
		about.addActionListener(this);

		menuBar.add(navigate);
		menuBar.add(help);

		navigate.add(logOut);
		navigate.add(mainMenu);
		navigate.add(goTo);

		help.add(helpContents);
		help.add(about);

		goTo.add(people);
		goTo.add(items);
		goTo.add(statistics);

		people.add(employees);
		people.add(customers);
		people.add(suppliers);
		people.add(subContractors);

		items.add(materials);
		items.add(projects);
		items.add(offers);
		items.add(requests);

	}

	public JMenuBar makeJMenuBar()
	{
		return menuBar;
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		
		for (Window frame : Window.getWindows())
			{
				if (frame.isActive())
				{
					frame.setVisible(false);
				}
			}
		if(e.getSource() == helpContents)
		{
			HelpUI help = HelpUI.getInstance();
			help.display();
		}
		else if(e.getSource() == about)
		{
			AboutUI about = AboutUI.getInstance();
			about.display();
		}
		else if(e.getSource() == statistics)
		{
			StatisticsUI stat = StatisticsUI.getInstance();
			stat.display();
		}
		else if(e.getSource() == employees)
			{
				EmployeeUI emp = EmployeeUI.getInstance();
				emp.display();
			}
		else if(e.getSource() == customers)
			{
				CustomerUI cus = CustomerUI.getInstance();
				cus.display();
				
			}
		else if(e.getSource() == suppliers)
			{
				SupplierUI sup = SupplierUI.getInstance();
				sup.display();
			}
		else if(e.getSource() == subContractors)
			{
				SubContractorUI sub = SubContractorUI.getInstance();
				sub.display();
			}
		else if(e.getSource() == materials)
			{
				ItemUI it = ItemUI.getInstance();
				it.display();
			}
		else if(e.getSource() == projects)
			{
				ProjectUI pr = ProjectUI.getInstance();
				pr.display();
			}
		else if(e.getSource() == offers)
			{
				OfferUI of = OfferUI.getInstance();
				of.display();
			}
		else if(e.getSource() == requests)
			{
				RequestsUI req = RequestsUI.getInstance();
				req.display();
			}
		else if(e.getSource() == logOut)
			{
				LoginUI log = LoginUI.getInstance();
				log.display();
			}
		else if(e.getSource() == mainMenu)
			{
				MenuUI menu = MenuUI.getInstance();
				menu.display();
			}
		
	}
}